#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time    : 2025/4/10 23:33
@Author  : HZP
@File    : app_service.py
"""
import base64
import secrets
import uuid
from dataclasses import dataclass

from injector import inject

from internal.model import App, Account
from pkg.password import hash_password
from pkg.sqlalchemy import SQLAlchemy


@inject
@dataclass
class AppService:
    """App服务类"""
    db: SQLAlchemy

    def create_app(self) -> App:
        """创建App"""
        with self.db.auto_commit():
            # 1.创建模型的实体类
            app = App(name="测试机器人", account_id=uuid.uuid4(), icon="",
                      description="这是一个简单的聊天机器人")
            # 2.将实体类添加到session会话中
            self.db.session.add(app)

        # account = Account()
        # account.name = "中电三公司智能语音"
        # account.email = "1184647618@qq.com"
        # account.phone = "15828679680"
        #
        # salt = secrets.token_bytes(16)
        # password_salt = base64.b64encode(salt).decode()
        # account.password_salt = password_salt
        #
        # password_hashed = hash_password("120823hu@", salt)
        # base64_password_hashed = base64.b64encode(password_hashed).decode()
        #
        # account.password = base64_password_hashed
        # with self.db.auto_commit():
        #     self.db.session.add(account)

        return app

    def get_app(self, id: uuid.UUID) -> App:
        """获取App"""
        return self.db.session.query(App).get(id)

    def update_app(self, app: App) -> App:
        """更新App"""
        with self.db.auto_commit():
            app.name = "云宝助手"
        return app

    def delete_app(self, id: uuid.UUID) -> App:
        """删除App"""
        with self.db.auto_commit():
            app = self.get_app(id)
            self.db.session.delete(app)
        return app
